from django.db import transaction
from django.db.models import Q
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext

from common.paginator import paginate

from projects.forms import ProjectForm, MemberQuickAdd
from projects.models import Project, Membership

def get_project(request, project_slug):
    project = get_object_or_404(Project, slug=project_slug)
    if project.private:
        if not request.user.is_authenticated():
            raise Http404
        if not request.user in project.members.all():
            raise Http404
    return project

def project_list(request, template_name='projects/project_list.html'):
    if request.user.is_authenticated():
        project_list = Project.objects.filter(Q(private=False) | Q(members=request.user))
    else:
        project_list = Project.objects.filter(private=False)
    projects = paginate(request, project_list)
    return render_to_response(template_name,
                              {'projects': projects},
                              context_instance=RequestContext(request))

@transaction.commit_on_success
def create_project(request, template_name='projects/project_form.html'):
    "This view is decorated with login_required."
    if request.method == 'POST':
        form = ProjectForm(request.POST)
        if form.is_valid():
            project = form.save(commit=False)
            project.creator = request.user
            project.save()
            member = Membership.objects.create(project=project,
                                               user=request.user)
            return HttpResponseRedirect(project.get_absolute_url())
    else:
        form = ProjectForm()
    return render_to_response(template_name,
                              {'form': form},
                              context_instance=RequestContext(request))

def project_detail(request, project_slug, template_name='projects/project_detail.html'):
    project = get_project(request, project_slug)
    return render_to_response(template_name,
                              {'project': project},
                              context_instance=RequestContext(request))

def member_list(request, project_slug, template_name='projects/member_list.html'):
    project = get_project(request, project_slug)
    members = paginate(request, project.membership_set.all())
    return render_to_response(template_name,
                              {'project': project,
                               'members': members},
                              context_instance=RequestContext(request))

@transaction.commit_on_success
def add_members(request, project_slug, template_name='projects/member_form.html'):
    project = get_project(request, project_slug)
    if request.user != project.creator:
        raise Http404
    if request.method == 'POST':
        form = MemberQuickAdd(request.POST)
        if form.is_valid():
            Membership.objects.create(user=form.cleaned_data['user'], project=project)
            return HttpResponseRedirect(project.get_absolute_url())
    else:
        form = MemberQuickAdd()
    return render_to_response(template_name,
                              {'form': form,
                               'project': project},
                              context_instance=RequestContext(request))